<?php 

function custom_resize_image($tmp_Name, $name)
{
	header ('Content-Type: image/png');


$property_id = 1; // for our little example here

define("_IMAGE_PATH","upload/");
// max dimensions allowed:
define("_IMAGE_WIDTH","500");
define("_IMAGE_HEIGHT","400");
define("_IMAGE_THUMB_WIDTH","100");
define("_IMAGE_THUMB_HEIGHT","75");

$UPLOAD_FOLDER_PATH = "upload/";
$IMAGE_WIDTH = "500";
$IMAGE_HEIGHT = "400";
$IMAGE_THUMB_WIDTH="100";
$IMAGE_THUMB_HEIGHT="100";

// grab the path to the temporary file (image) that the user uploaded

$fullFile =$UPLOAD_FOLDER_PATH.$photo; 
$photo = $fullFile;

// check if it exists
	if(file_exists($fullFile))
	{
	    //the real image file name
	    $real_name = strtolower($name);
	    // image type based on image file name extension:
	    if(strstr($real_name,".png")){
	        $image_type = "png";
	    }else if(strstr($real_name,".jpg")){
	        $image_type = "jpg";
	    }else if(strstr($real_name,".gif")){
	        $image_type = "gif";
	    }else{
	        die("Unsupported image type");
	    }
	    
	    
	    // find the next image name we are going to save
	    $x=1;
	    /*while(true){
	        $image_name = $IMAGE_."${property_id}/${x}.jpg";
	        if(!is_file($image_name))break;
	        $x++;
	    }*/
	    
	    // start processing the main bigger image:
	    $max_width = $IMAGE_WIDTH; $max_height = $IMAGE_HEIGHT;
	    $size = getimagesize($fullFile);
	    
	    $width = $size[0];
	    $height = $size[1];
	    $x_ratio = $max_width / $width;
	    $y_ratio = $max_height / $height;
	    
	    if(($width <= $max_width)&&($height <= $max_height)){
	        $tn_width = $width;
	        $tn_height = $height;
	    }else{
	        if(($x_ratio * $height) < $max_height){
	            $tn_height = ceil($x_ratio * $height);
	            $tn_width = $max_width;
	        }else{
	            $tn_width = ceil($y_ratio * $width);
	            $tn_height = $max_height;
	        }
	    }
	    switch($image_type){
	        case "png": $src=imagecreatefrompng($name); break;
	        case "jpg": $src=imagecreatefromjpeg($name); break;
	        case "gif": $src=imagecreatefromgif($name); break;
	    }
	    // destination resized image:
	    $dst = imagecreatetruecolor($tn_width, $tn_height);
	    // resize original image onto $dst
	    imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	    // write the final jpeg image..
	    imagejpeg($dst, $image_name, 100) or die("Error: your photo
	    has not been saved. Please contact the administrator");
	    // time to clean up
	    imagedestroy($src);
	    imagedestroy($dst);
	    
	    
	    // and now we do it alll again for the thumbnail:
	    $max_width = _IMAGE_THUMB_WIDTH; $max_height = _IMAGE_THUMB_HEIGHT;
	    $size = GetImageSize($photo);
	    $width = $size[0];
	    $height = $size[1];
	    $x_ratio = $max_width / $width;
	    $y_ratio = $max_height / $height;
	    if(($width <= $max_width)&&($height <= $max_height)){
	        $tn_width = $width;
	        $tn_height = $height;
	    }else{
	        if(($x_ratio * $height) < $max_height){
	            $tn_height = ceil($x_ratio * $height);
	            $tn_width = $max_width;
	        }else{
	            $tn_width = ceil($y_ratio * $width);
	            $tn_height = $max_height;
	        }
	    }
	    switch($image_type){
	        case "png": $src=imagecreatefrompng($photo); break;
	        case "jpg": $src=imagecreatefromjpeg($photo); break;
	        case "gif": $src=imagecreatefromgif($photo); break;
	    }
	    $dst = imagecreatetruecolor($tn_width, $tn_height);
	    imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	    $thumbfile = $image_name . ".thumb.jpg";
	    if(file_exists($thumbfile))unlink($thumbfile);
	    imagejpeg($dst, $thumbfile, 100) or die("Error: your photo thumb has not been saved.
	       Please contact the administrator");
	    imagedestroy($src);
	    imagedestroy($dst);
	}
}
?>